package com.learn.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.learn.springcloud.entity.Dept;
import com.learn.springcloud.service.DeptService;

@RestController
@RequestMapping("/dept")
public class DeptController {

	@Autowired
	private DeptService deptService;
	
	@Autowired
	private DiscoveryClient client;
	
	@PostMapping("/add")
	public boolean add(@RequestBody Dept dept){
		return deptService.add(dept);
	}
	
	@GetMapping("/get/{id}")
	public Dept get(@PathVariable("id") Long id){
		return deptService.get(id);
	}
	
	@GetMapping("/list")
	public List<Dept> list(){
		return deptService.list();
	}
	
	@GetMapping("/discovery")
	public Object discovery(){
		List<String> list = client.getServices();
		System.out.println("==========="+list);
		
		List<ServiceInstance> serviceList = client.getInstances("MICROSERVICECLOUD-DEPT");
		for(ServiceInstance service:serviceList){
			System.out.println(service.getServiceId()+"\t"+service.getHost()+"\t"+service.getPort()+"\t"+service.getUri());
		}
		return this.client;
	}
}
